package com.vietnam.power.service.impl;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vietnam.power.dao.CacheDao;
import com.vietnam.power.service.CacheService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class CacheServiceImpl implements CacheService {

	@Autowired
	private CacheDao cacheDao;
	
	@Autowired
	private EhCacheCacheManager ehCacheCacheManager;

	public void clearAllCache() {
		// clear spring cache
		CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
		String[] cacheNames = cacheManager.getCacheNames();
		for (String cacheName : cacheNames) {
			Cache cache = cacheManager.getCache(cacheName);
			cache.removeAll();
		}
		// clear hibernate cache
		cacheDao.clearAllCache();
	}
}
